/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.eric.server.test;

import com.eric.server.logic.packet.ChatToGroupPacket;
import com.eric.server.logic.packet.NoticePacket;
import com.eric.server.logic.packet.UserOutPacket;
import com.eric.server.logic.packet.UserRegisterPacket;
import com.eric.server.packet.Packet;
import com.eric.server.packet.PacketType;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Scanner;


public class EchoClientHandler extends ChannelInboundHandlerAdapter {

    private ChannelHandlerContext client ;
    private Speak speak ;


    public EchoClientHandler() {

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);

        speak.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        client = ctx;
        speak = new Speak();
        speak.start();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
        Packet packet = (Packet)msg;
        String result = null;
        switch (packet.getPacketType()){
            case NoticeClient:
                NoticePacket noticePacket = (NoticePacket)packet;
                result = "通知: "+noticePacket.msg;
                break;
            case UserOut:
                UserOutPacket userOutPacket = (UserOutPacket)packet;
                result = "强制退出: "+userOutPacket.reason;
                ctx.close();
                speak.close();
                break;
            case ChatToGroup:
                ChatToGroupPacket chatToGroupPacket = (ChatToGroupPacket)packet;
                result = chatToGroupPacket.user +" 说 : "+chatToGroupPacket.msg;
                break;
        }
        System.out.println("from server : "+result);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
//       ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    class Speak extends Thread{
        Scanner scanner = new Scanner(System.in);
        @Override
        public void run() {

            try{
                String line ;
                while (scanner.hasNext()){
                    line = scanner.next();
                    String[] args = line.split(",");
                    if (args.length < 3) continue;
                    int type = Integer.parseInt(args[0]);
                    Class<? extends Packet> packetClz = PacketType.getPacketClassBy(type);
                    Packet packet = null;
                    if(packetClz != null){
                        packet = packetClz.newInstance();
                    }
                    if(packet == null)continue;
                    packet = packet.obtain(wrap(args));
                    client.writeAndFlush(packet);

                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }

        public void close(){
            scanner.close();
        }

        private String [] wrap(String []s){
            String []result = new String[s.length-1];
            for (int i=1 ; i<s.length ;i++){
                result[i-1] = s[i];
            }
            return result;
        }
    }
}
